package com.jiaai.cloud.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.page.CustomPage;
import com.jiaai.cloud.base.util.RestResultGenerator;
import com.jiaai.cloud.system.entity.TRole;
import com.jiaai.cloud.system.entity.TRoleSearch;
import com.jiaai.cloud.system.service.ITRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 角色表信息 前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-08-24
 */
@RestController
@RequestMapping("/api/manage/tRole")
public class TRoleController {
    @Autowired
    private ITRoleService tRoleService;

    //添加
    @RequestMapping( value = "/addRole" ,method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})

    public  Object addRole( TRole role) throws ServiceException{
        try {
            role.setCreateTime(new Date());
            return RestResultGenerator.genSuccessResult(tRoleService.insertOrUpdate(role));
        } catch (Exception e) {
            throw new ServiceException("添加失敗", e.getMessage());
        }
    }

    //查所有带分页
    @RequestMapping(value ="/findByAll",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object findByAll(TRoleSearch search) throws ServiceException {
        try {
            Page page=search.getPagePlus();
            EntityWrapper wrapper=new EntityWrapper();
            if(!StringUtils.isEmpty(search.getRoleName())){
                wrapper.like("roleName",search.getRoleName());
            }
            tRoleService.selectPage(page,wrapper);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
        } catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }


    //通过Id查找所有
    @RequestMapping(value ="/findById/{id}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findById(@PathVariable("id") Integer id ) throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult( tRoleService.selectById(id));
        } catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //    删除
    @RequestMapping(value = "/deleteId/{id}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object deleteId(@PathVariable("id") Integer id) throws ServiceException {
        try {

            tRoleService.deleteById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //可以添加，有的话可以修改
    @RequestMapping( value ="/edit" ,method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public  Object edit(@RequestBody TRole role) throws ServiceException{
        try {
//            role.setCreateTime(new Date());
            tRoleService.insertOrUpdate(role);
            return null;
        } catch (Exception e) {
            throw new ServiceException("更改失敗", e.getMessage());
        }
    }



}
